package com.ravi.service.impl;

import com.ravi.queue.PatientCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.LinkedBlockingQueue;

@Service
public class AsyncTaskService {

	@Autowired
	private PatientCollection patientCollection;

	@Async
	public void enqueue(Integer patientId, String roomCode) {
		LinkedBlockingQueue<Integer> queue = patientCollection.manager.get(roomCode);
		try {
			queue.put(patientId);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Async
	public Integer dequeue(String roomCode) {
		LinkedBlockingQueue<Integer> queue = patientCollection.manager.get(roomCode);
		Integer target = null;
		try {
			 target = queue.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return target;
	}
}
